package main

import (
	"log"

	/*"io/ioutil"
	"os"*/

	print "github.com/alexbrainman/printer"
	"github.com/axgle/mahonia"
	"github.com/golang/freetype"
	"github.com/golang/freetype/truetype"
)

var (
	backgroundWidth  = 650
	backgroundHeight = 150
	utf8FontFile     = "wqy-zenhei.ttf"
	utf8FontSize     = float64(35.0)
	spacing          = float64(1.5)
	dpi              = float64(72)
	ctx              = new(freetype.Context)
	utf8Font         = new(truetype.Font)
	red              = color.RGBA{255, 0, 0, 255}
	blue             = color.RGBA{0, 0, 255, 255}
	white            = color.RGBA{255, 255, 255, 255}
	black            = color.RGBA{0, 0, 0, 255}
	background       *image.RGBA
	// more color at https://github.com/golang/image/blob/master/colornames/table.go
)

func main() {

	Print("优诺", "成飞大道", "牛腩", "15.60", "斤")
}

func Print(title, address, goods_name, weight_goods_num, unit string) {
	name, err := print.Default() // returns name of Default Printer as string
	if err != nil {
		log.Fatal(err)
	}
	log.Println(name)
	p, err := print.Open(name) // Opens the named printer and returns a *Printer
	if err != nil {
		log.Fatal(err)
	}
	log.Println(p)
	err = p.StartDocument("test", "text") // test: doc name, text: doc type
	if err != nil {
		log.Fatal(err)
	}
	/*err = p.StartPage() // begin a new page
	if err != nil {
		log.Fatal(err)
	}*/

	/*f, err := os.Open("src/1.txt")
	if err != nil {
		log.Println(err)
	}
	content, err := ioutil.ReadAll(f)
	enc := mahonia.NewEncoder("gbk")
	log.Println(enc.ConvertString(string(content)))*/

	content := "\r\n  " + title + " - \r\n"
	content += "\r\n  " + address + " \r\n"
	content += "\r\n  " + weight_goods_num + unit + "   2018-08-31"
	enc := mahonia.NewEncoder("gbk")
	content = enc.ConvertString(content)
	/*content := "asdf; 奔跑"
	enc := mahonia.NewEncoder("gbk")
	content = enc.ConvertString(content)*/
	n, err := p.Write([]byte(content)) // Send some text to the printer
	if err != nil {
		log.Fatal(err)
	}
	log.Println(n)
	/*err = p.EndPage() // begin a new page
	if err != nil {
		log.Fatal(err)
	}*/
	err = p.EndDocument() // test: doc name, text: doc type
	if err != nil {
		log.Fatal(err)
	}

	err = p.Close() // close the resource
	if err != nil {
		log.Fatal(err)
	}
}
